﻿var $ = jQuery.noConflict();
var JustMarvel = JustMarvel || {};

(function ($) {
    JustMarvel.initialize = {
        init: function () {
            JustMarvel.initialize.creatCaptcha();
            JustMarvel.initialize.login();
        },
        creatCaptcha: function () {
            var nums = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
            ];
            var verVal = drawCode();
            // 绘制验证码
            function drawCode(captchaStr) {
                var canvas = document.getElementById("verifyCanvas"); //获取HTML端画布
                var context = canvas.getContext("2d"); //获取画布2D上下文
                context.fillStyle = "cornflowerblue"; //画布填充色
                context.fillRect(0, 0, canvas.width, canvas.height); //清空画布
                context.fillStyle = "white"; //设置字体颜色
                context.font = "25px Arial"; //设置字体
                var rand = new Array();
                var x = new Array();
                var y = new Array();
                for (var i = 0; i < 4; i++) {
                    rand.push(rand[i]);
                    rand[i] = nums[Math.floor(Math.random() * nums.length)]
                    x[i] = i * 20 + 10;
                    y[i] = Math.random() * 20 + 20;
                    context.fillText(rand[i], x[i], y[i]);
                }
                captchaStr = rand.join('');
                //画3条随机线
                for (var i = 0; i < 3; i++) {
                    drawline(canvas, context);
                }

                // 画30个随机点
                for (var i = 0; i < 30; i++) {
                    drawDot(canvas, context);
                }
                convertCanvasToImage(canvas);
                //console.log(captchaStr);
                return captchaStr;
            }

            // 随机线
            function drawline(canvas, context) {
                context.moveTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height)); //随机线的起点x坐标是画布x坐标0位置，y坐标是画布高度的随机数
                context.lineTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height)); //随机线的终点x坐标是画布宽度，y坐标是画布高度的随机数
                context.lineWidth = 0.5; //随机线宽
                context.strokeStyle = 'rgba(50,50,50,0.3)'; //随机线描边属性
                context.stroke(); //描边，即起点描到终点
            }
            // 随机点(所谓画点其实就是画1px像素的线，方法不再赘述)
            function drawDot(canvas, context) {
                var px = Math.floor(Math.random() * canvas.width);
                var py = Math.floor(Math.random() * canvas.height);
                context.moveTo(px, py);
                context.lineTo(px + 1, py + 1);
                context.lineWidth = 0.2;
                context.stroke();

            }
            // 绘制图片
            function convertCanvasToImage(canvas) {
                document.getElementById("verifyCanvas").style.display = "none";
                var image = document.getElementById("code_img");
                image.src = canvas.toDataURL("image/png");
                return image;
            }

            // 点击图片刷新
            document.getElementById('code_img').onclick = function () {
                resetCode();
            }

            function resetCode() {
                $('#verifyCanvas').remove();
                $('#code_img').before('<canvas width="100" height="40" id="verifyCanvas"></canvas>')
                verVal = drawCode();
            }

        },

        login: function () {
            var name = $('#userName').val(),
                psd = $('#password').val(),
                captcha = $('#captcha').val(),
                $messager = $(".messager");

            //$('#loginsubmit').on('click', function () {

            //    console.log(name, psd);
            //});
            $("ul li a.type").on('click', function () {
                console.log(111);
            });
            //回车登录
            $(document).keyup(function(event){
                if(event.keyCode ==13){
                    $("#loginBtn").trigger("click");
                }
            });

            $("#loginBtn").on('click', function () {
                $messager.empty().css({color:"#fff"});

                var name = $('#userName').val(),
                    psd = $('#password').val(),
                    captcha = $('#captcha').val(),
                    type = $('#type').val(),
                    rightList = [];
                //console.log(name, psd);
                if (name == null || name == undefined || name == '') {
                    $messager.html('用户名不能为空').css({ color: '#f00' });
                    return false;
                }
                else if (psd == null || psd == undefined || psd == '') {
                    $messager.html('请填写密码').css({ color: '#f00' });
                    return false;
                }
                else if (captcha == null || captcha == undefined || captcha == '') {
                    $messager.html('图形验证码不能为空').css({ color: '#f00' });
                    return false;
                }
                else{
                    $.ajax({
                        url: "/admin/login/login",
                        type: "POST",
                        async: false,
                        dataType: "json",
                        data: { "username": name, "password": psd,"type":type },
                        timeout: 5000,
                        success: function (data) {
                            // console.log(data);
                            if (data.code == 200) {
                                var date = new Date();
                                date.setTime(date.getTime() + 600 * 1000);
                                if($(".checkbox input[type=checkbox]").is(":checked")){
                                    $.cookie('login', true, { expires: date });
                                    $.cookie('username', name);
                                }

                                /*for (var i = 0; i < data.result.rightList.length; i++) {
                                    if (data.result.rightList[i].isCheck == 0) {
                                        rightList.push(data.result.rightList[i].code);
                                    };
                                };
                                rightList = rightList.join(',');
                                console.log(rightList);
                                $.cookie('right', rightList);*/

                                $messager.html('登录成功，正在为您跳转...').css({ color: "#92bb21" });// #b2f300
                                setTimeout(function () {
                                    window.location.replace('/');
                                }, 500);
                            } else {
                                $messager.html('登录失败，请检查您的用户名和密码').css({ color: "#ff6600" });
                                //$('#userName').val('');
                               // $('#password').val('');
                            };
                        },
                        error: function () {
                            $messager.html('登录失败，请稍后再试！').css({ color: "#ff6600" });
                            //$('#userName').val('');
                            //$('#password').val('');
                        }
                    });
                    //$messager.html('登录中...').css({ color: "#b2f300" });
                    // 测试用
                    // $.cookie('login', true);
                    // $.cookie('username', name);
                    /* setTimeout(function () {
                        $messager.html('登录成功，正在为您跳转...').css({ color: "#b2f300" });
                    }, 500);
                    setTimeout(function () {
                        window.location.replace('/admin/index/index')
                    }, 1500);*/
                    /*测试用*/
                };
            });
        }


    };


    //DOM加载完成
    JustMarvel.documentOnReady = {
        init: function () {
        }
    };
    //文档加载完成
    JustMarvel.documentOnLoad = {
        init: function () {
            JustMarvel.initialize.init();
        }
    };
    //全局参数定义
    var currenttime = function () {
        var currenttime = new Date().toLocaleTimeString().replace(/^\D*/, '');
        return currenttime;
    };

    //全局页面DOM定义
    var $window = $(window),
        $body = $('body'),
        $mainbg = $('.main-bg');

    var captchaStr = '';//验证码字符串

    $(document).ready(JustMarvel.documentOnReady.init);
    $window.load(JustMarvel.documentOnLoad.init);

})(jQuery);